<?php
namespace app\controller;

use app\BaseController;
use app\model\Standard;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{
    public function index(){
        $data['day'] = date('Y-m-d');
        $count = Db::name('order')->where('create_time','>',$data['day'])->count();
        $count = $count + 1001;
        $count = substr($count,1);
        $data['order_no'] = 'LA'.date('Ymd').$count;
        $data['attr'] = Db::name('falan')->field('attr')->group('attr')->select();
        $data['ss'] = Db::name('falan')->field('ss')->group('ss')->select();
        $data['pressure'] = Db::name('falan')->field('pressure')->group('pressure')->select();
        $data['piping'] = Db::name('falan')->field('piping')->group('piping')->select();
        View::assign('data',$data);
        return View::fetch('/index/index');
    }

    public function falan(){
        $post = input('post.');
        $where = [];
        !empty($post['attr']) && $where[] = ['attr','=',$post['attr']];
        !empty($post['ss']) && $where[] = ['ss','=',$post['ss']];
        !empty($post['pressure']) && $where[] = ['pressure','=',$post['pressure']];
        !empty($post['piping']) && $where[] = ['piping','=',$post['piping']];
        !empty($post['type_name']) && $where[] = ['type_name','=',$post['type_name']];
        $list = Db::name('falan')
            ->field('min(price) AS price,attr,pressure')
            ->where($where)
            ->where('attr','not like','')
            ->where('price','>',0)
            ->group('pressure,attr')
            ->select()
            ->toArray();
        $pressure = Db::name('falan')
            ->field('pressure')
            ->where($where)
            ->group('pressure')
            ->select()
            ->toArray();
        $pressure = array_column($pressure,'pressure');
        asort($pressure);
        $temp = [];
        foreach ($list AS $key => $value)
            $temp[$value['attr']][$value['pressure']] = $value['price'];
        foreach ($temp AS $key => $value) {
            foreach ($pressure AS $index => $item) {
                if (empty($temp[$key][$item])) $temp[$key][$item] = 0;
            }
            asort($temp[$key]);
        }
        return json(['code' => 200,'msg' => '','data' => ['list' => $temp,'pressure' => $pressure]]);
    }

    public function material_quality_list(){

    }

    public function company(){
        $where = [];
        $post = input('post.');
        $where[] = ['company_name','like',$post['company']];
        $company = Db::name('company')->where($where)->find();
        return json(['code' => 200,'msg' => '','data' => $company]);
    }

    public function fdata(){
        $map = $where = [];
        $post = input('post.');
        $array = [
            '平焊法兰' => 'PL',
            '带颈平焊法兰' => 'SO',
            '对焊带颈法兰' => 'WIN',
            '法兰盖' => NULL,
            '承插焊法兰' => NULL,
            '螺纹法兰' => NULL,
            '对焊环松套法兰' => NULL
        ];
        !empty($post['attr']) && $map[] = $where[] = ['attr','like',$post['attr']];
        !empty($post['ss']) && $where[] = ['ss','like',$post['ss']];
        !empty($post['pressure']) && $where[] = ['pressure','like',$post['pressure']];
        !empty($post['piping']) && $where[] = ['piping','like',$post['piping']];
        !empty($post['name']) && $map[] = ['type','like',$array[$post['name']]];
        !empty($post['material']) && $map[] = ['ma','=',$post['material']];
        if (!empty($post['name']) && $post['name'] == '对焊带颈法兰(WN)')
            !empty($post['name']) && $map[] = ['pressure','like',$post['name']];
        $list['falan'] = Db::name('falan')->where($where)->limit(20)->select();
        $list['price'] = Db::name('falan_price')->where($map)->limit(20)->select();
        return json(['code' => 200,'msg' => '','data' => $list]);
    }

    public function cdata(){
        $company_name = input('company_name');
        $list = Db::name('company')->where('company_name','like','%'.$company_name.'%')->limit(20)->select();
        return json(['code' => 200,'msg' => '','data' => $list]);
    }

    public function set_get_cache(){
        $name = input('name');
        $data = input('data');
        $message = [];
        if (empty($data))
            $message = cache($name);
        else
            cache($name,$data);
        return json(['code' => 200,'data' => $message],200);
    }

    public function standard(){
        $data = Standard::select();
        return json(['code' => 200,'data' => $data],200);
    }

    public function volume(){
        $type_name = input('type_name');
        $volume = input('post.volume');
        $data = (new Standard())
            ->where('type_name', $type_name)
            ->find();
        // 构建原始字符串表达式
        $temp = $data->toArray();
        $expression = $data->algorithm;
        // 创建一个关联数组来映射变量名和它们的值
        $variables = $volume;
        $variables['PI'] = 3.1415926;
        $result['weight_algorithm'] = $expression;
        foreach ($variables AS $key => $value)
            $variables[strtoupper($key)] = $value;
        $expression = strtr(strtoupper($expression), $variables);
        // 使用eval计算表达式
        $result['weight_result_algorithm'] = $expression;
        $result['weight'] = eval("return ($expression);");
        return json([
            'code' => 200,
            'data' => $result
        ], 200);
    }

    public function calculate_result(){
        try {
            $array = [];
            $id = input('id');
            $dd = input('dd');
            $standard_number = input('standard_number');
            $data = (new Standard())
                ->where('id', $id)
                ->find();
            // 构建原始字符串表达式
            $temp = $data->toArray();
            $expression = $data->algorithm;
            // 创建一个关联数组来映射变量名和它们的值
            $variables = $standard_number;
            $variables['PI'] = 3.1415926;
            $result['weight_algorithm'] = $expression;
            // 使用strtr替换字符串中的变量，因为strtr会处理整个单词而不是子字符串
            foreach ($variables AS $key => $value)
                $variables[strtoupper($key)] = $value;
            $expression = strtr(strtoupper($expression), $variables);
            // 使用eval计算表达式
            $result['weight_result_algorithm'] = $expression;
            $weight = eval("return ($expression);");
            $expression = $data->blank_algorithm;
            $expression = strtr(strtoupper($expression), $variables);
            $blank_weight = eval("return ($expression);");
            $weight = bcadd($weight,0,4);
            $blank_weight = bcadd($blank_weight,0,4);
            $result['weight_result_blank_algorithm'] = $expression;
            foreach ($dd AS $key => $value){
                if (is_array($value))continue;
                $array[$key] = $value;
            }
            $array['PI'] = 3.1415926;
            $array = array_merge($standard_number,$array);
//            $result['weight'] = $array['WEIGHT'] = $weight;
            $result['weight'] = $weight;
            $result['blank_weight'] = $array['WEIGHT'] = $blank_weight;
            foreach ($array AS $key => $value)
                $array[strtoupper($key)] = $value;
            if (!empty($temp['other_algorithm'])) {
                $temp['other_algorithm'] = json_decode(json_encode($temp['other_algorithm']), true);
                $algorithm_temp = array_column($temp['other_algorithm'], 'algorithm', 'vara');
                $algorithm = [];
                foreach ($algorithm_temp AS $key => $value)
                    $algorithm[strtoupper($key)] = strtoupper('(' . $value . ')');
                // 获取所有键的数组
                $keysArray = array_keys($algorithm);
                // 获取所有值的数组
                $valuesArray = array_values($algorithm);
//            var_dump($keysArray,$valuesArray);
                foreach ($data->other_algorithm AS $key => $value) {
                    $temp_algorithm = str_replace($keysArray, $valuesArray, $value['algorithm']);
//                var_dump($temp_algorithm);
                    foreach ($algorithm AS $index => $item)
                        $temp_algorithm = str_replace($index, $item, $temp_algorithm);
                    $value['result_algorithm'] = strtr(strtoupper($temp_algorithm), $array);
                    $value['result'] = eval("return ({$value['result_algorithm']});");
                    $value['result'] = bcadd($value['result'], 0, 4);
                    $value['algorithm'] = $temp_algorithm;
                    $result['other_array'][] = $value;
                }
                $result['vara'] = $array;
            }
            return json([
                'code' => 200,
                'data' => $result
            ], 200);
        }catch (\Exception $e){
            return json(['code' => 400,'msg' => $e->getTraceAsString()],200);
        }
        //D8/2*D8/2*G8*3.1415926*MD/1000/1000-(E8-F8*2)/2*(E8-F8*2)/2*K8*3.1415926*MD/1000/1000+1/3*3.1415926*(K8-G8-H8)*(I8*I8/4+I8*E8/4+E8*E8/4)/1000000*MD+J8*J8/4*H8*3.1415926*MD/1000/1000-(E8-F8*2)*(E8-F8*2)/4*H8*3.1415926*MD/1000/1000-G5*G5/4*3.1415926*MD*G8/1000/1000*I5
    }
}